<?php


/**
* CakePHP Schema Plugin
* http://cake-schema-plugin.googlecode.com
*
* @package schema
* @subpackage schema.models.datasources
* @author Ivan Chistyakov <zetruger@gmail.com>
* @version $Id: schema_source.php 129 2010-10-25 14:17:58Z zetruger@gmail.com $
* @link $HeadURL: http://cake-schema-plugin.googlecode.com/svn/trunk/models/datasources/schema_source.php $
* @license MIT License (http://opensource.org/licenses/mit-license.php)
*/


$ds = &ConnectionManager::getDataSource('default');
eval('class DefaultDataSource extends ' . get_class($ds) . ' {}');


class SchemaSource extends DefaultDataSource {

var $description = 'Schema Source';

var $_ds = NULL;

var $_schema = NULL;

function __construct($config = array()) {
	$this->_ds = &ConnectionManager::getDataSource('default');
	parent::__construct($this->_ds->config);
	$this->_schema = new DbSchema();
	foreach (array_keys($this->_schema->tables) as $tableName) {
		$this->execute($this->createSchema($this->_schema, $tableName));
	}
}

function listSources() {
	return array_keys($this->_schema->tables);
}

function renderStatement($type, $data) {
	$sql = parent::renderStatement($type, $data);
	if ($type == 'schema') {
		$sql = preg_replace('~^\s*CREATE\s+TABLE~i', 'CREATE TEMPORARY TABLE', $sql);
	}
	return $sql;
}

}


App::import('Model', 'CakeSchema', FALSE);


class DbSchema extends CakeSchema {

var $name = 'Db';

var $schema_tables = array(
	'id' => array('type' => 'integer', 'null' => FALSE, 'default' => NULL, 'key' => 'primary'),
	'name' => array('type' => 'string', 'null' => FALSE),
	'charset' => array('type' => 'string', 'null' => FALSE, 'default' => 'utf8'),
	'collate' => array('type' => 'string', 'null' => FALSE, 'default' => 'utf8_general_ci'),
	'engine' => array('type' => 'string', 'null' => FALSE, 'default' => 'MyISAM'),
	'tree' => array('type' => 'boolean', 'null' => FALSE),
	'position' => array('type' => 'integer', 'null' => FALSE),
	'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => 1))
);

var $schema_columns = array(
	'id' => array('type' => 'integer', 'null' => FALSE, 'default' => NULL, 'key' => 'primary'),
	'schema_table_id' => array('type' => 'integer', 'null' => FALSE, 'key' => 'index'),
	'name' => array('type' => 'string', 'null' => FALSE),
	'type' => array('type' => 'string', 'null' => FALSE),
	'null' => array('type' => 'boolean', 'null' => FALSE),
	'default' => array('type' => 'string', 'null' => TRUE, 'default' => NULL),
	'length' => array('type' => 'integer', 'null' => FALSE),
	'key' => array('type' => 'string', 'null' => FALSE),
	'key_name' => array('type' => 'string', 'null' => FALSE),
	'translate' => array('type' => 'boolean', 'null' => FALSE),
	'field_type' => array('type' => 'string', 'null' => FALSE),
	'label' => array('type' => 'string', 'null' => FALSE),
	'position' => array('type' => 'integer', 'null' => FALSE),
	'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => 1), 'row_id' => array('column' => 'schema_table_id', 'unique' => 0))
);

}


?>